package com.bankcomm.ui.specialwidget;

import java.util.Vector;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 无缝TextView
 */
public class MySeamLessTextView extends TextView {

	/* 声明Paint对象 */
	private Paint mPaint = null;
	private int m_iTextHeight;
	private String displayText = "";
	private int textHeight;

	public MySeamLessTextView(Context context) {
		super(context);
	}

	public MySeamLessTextView(Context context, AttributeSet set) {
		super(context, set);
	}

	public void init(Context context, String text, int width) {
		displayText = text;
		textHeight = width;

		/* 构建对象 */
		m_iTextHeight = 2000;
		mPaint = new Paint();
		mPaint.setColor(Color.BLACK);
		mPaint.setStrokeWidth(40);
		mPaint.setTextSize(20);

		FontMetrics fm = mPaint.getFontMetrics();
		int m_iFontHeight = (int) Math.ceil(fm.descent - fm.top) + 4;

		int line = 0;
		int w = 0;
		for (int i = 0; i < displayText.length(); i++) {
			char ch = text.charAt(i);
			float[] widths = new float[1];
			String srt = String.valueOf(ch);
			mPaint.getTextWidths(srt, widths);

			if (ch == '\n') {
				line++;
				w = 0;
			} else {
				w += (int) (Math.ceil(widths[0]));
				if (w > width) {
					line++;
					i--;
					w = 0;
				} else {
					if (i == (displayText.length() - 1)) {
						line++;
					}
				}
			}
		}
		m_iTextHeight = line * m_iFontHeight + 5;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		/* 设置背景颜色 */
		canvas.drawColor(Color.WHITE);

		mPaint.setAntiAlias(true);

		char ch;
		int w = 0;
		int istart = 0;
		int m_iFontHeight;
		int m_iRealLine = 0;
		int x = 0;
		int y = 25;
		Vector<String> m_String = new Vector<String>();
		FontMetrics fm = mPaint.getFontMetrics();

		m_iFontHeight = (int) Math.ceil(fm.descent - fm.top) + 4;

		for (int i = 0; i < displayText.length(); i++) {
			ch = displayText.charAt(i);
			float[] widths = new float[1];
			String srt = String.valueOf(ch);
			mPaint.getTextWidths(srt, widths);

			if (ch == '\n') {
				m_iRealLine++;
				m_String.addElement(displayText.substring(istart, i));
				istart = i + 1;
				w = 0;
			} else {
				w += (int) (Math.ceil(widths[0]));
				if (w > textHeight) {
					m_iRealLine++;
					m_String.addElement(displayText.substring(istart, i));
					istart = i;
					i--;
					w = 0;
				} else {
					if (i == (displayText.length() - 1)) {
						m_iRealLine++;
						m_String.addElement(displayText.substring(istart, displayText.length()));
					}
				}
			}
		}
		m_iTextHeight = m_iRealLine * m_iFontHeight + 5;
//		canvas.setViewport(textHeight, textHeight);
		for (int i = 0, j = 0; i < m_iRealLine; i++, j++) {
			canvas.drawText((String) (m_String.elementAt(i)), x, y + m_iFontHeight * j, mPaint);
		}
	}

	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int measuredHeight = measureHeight(heightMeasureSpec);
		int measuredWidth = measureWidth(widthMeasureSpec);
		this.setMeasuredDimension(measuredWidth, measuredHeight);
		this.setLayoutParams(new LinearLayout.LayoutParams(measuredWidth, measuredHeight));
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}

	private int measureHeight(int measureSpec) {
		int specMode = MeasureSpec.getMode(measureSpec);
		int specSize = MeasureSpec.getSize(measureSpec);

		// Default size if no limits are specified.
		int result = m_iTextHeight;
		if (specMode == MeasureSpec.AT_MOST) {
			// Calculate the ideal size of your control within this maximum size.
			// If your control fills the available space return the outer bound.
			result = specSize;
		} else if (specMode == MeasureSpec.EXACTLY) {
			// If your control can fit within these bounds return that value.
			result = specSize;
		}
		return result;
	}

	private int measureWidth(int measureSpec) {
		int specMode = MeasureSpec.getMode(measureSpec);
		int specSize = MeasureSpec.getSize(measureSpec);

		// Default size if no limits are specified.
		int result = 500;
		if (specMode == MeasureSpec.AT_MOST) {
			// Calculate the ideal size of your control within this maximum size.
			// If your control fills the available space return the outer bound.
			result = specSize;
		} else if (specMode == MeasureSpec.EXACTLY) {
			// If your control can fit within these bounds return that value.
			result = specSize;
		}
		return result;
	}

}
